﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NObsidian.Containers;
using NObsidian.Envelopes;

namespace NObsidian.Managers
{
    public class WikiPageManager : ManagerBase
    {
        public WikiPageManager(ObsidianProxy proxy)
            : base(proxy)
        {
        }

        public List<WikiPage> GetCampaignWikiPagesIndex(string campaignId)
        {
            string wikiindex;
            string url;

            url = ComposeWikiIndexURL(campaignId);
            wikiindex = _proxy.GetObsidianData(url);

            return _proxy.GetObjectFromJson<List<WikiPage>>(wikiindex);
        }

        public WikiPage GetCampaignWikiPage(string campaignId, string wikiPageId)
        {
            string wiki;
            string url;

            url = ComposeWikiURL(campaignId, wikiPageId);
            wiki = _proxy.GetObsidianData(url);

            return _proxy.GetObjectFromJson<WikiPage>(wiki);
        }

        // POST 
        public WikiPage CreateCampaignWikiPage(string campaignId, WikiPage wikiPage)
        {
            WikiPage c;
            WikiPageEnvelope envelope;
            string url;
            string response;

            url = ComposeWikiIndexURL(campaignId);
            c = wikiPage.GetMinimumUpdateObject<WikiPage>();
            envelope = new WikiPageEnvelope { WikiPage = c };

            response = _proxy.CreateObsidianData(url, _proxy.GetJSONFromObject<WikiPageEnvelope>(envelope));
            c = _proxy.GetObjectFromJson<WikiPage>(response);

            return c;
        }
        
        public WikiPage UpdateCampaignWikiPage(string campaignId, string wikiPageId, WikiPage wikiPage)
        {
            WikiPage c;
            WikiPageEnvelope envelope;
            string url;
            string response;

            url = ComposeWikiURL(campaignId, wikiPageId);
            c = wikiPage.GetMinimumUpdateObject<WikiPage>();
            envelope = new WikiPageEnvelope { WikiPage = c };

            response = _proxy.UpdateObsidianData(url, _proxy.GetJSONFromObject<WikiPageEnvelope>(envelope));
            c = _proxy.GetObjectFromJson<WikiPage>(response);

            return c;
        }

        public void DeleteCampaignWikiPage(string campaignId, string wikiPageId)
        {
            string url;

            url = ComposeWikiURL(campaignId, wikiPageId);
            _proxy.DeleteObsidianData(url);
        }

        private string ComposeWikiIndexURL(string id)
        {
            StringBuilder sb = new StringBuilder();

            return sb.Append(CAMPAIGNS_URL).Append(id).Append("/wikis.json").ToString();
        }

        private string ComposeWikiURL(string campaignId, string wikiId)
        {
            StringBuilder sb = new StringBuilder();

            return sb.Append(CAMPAIGNS_URL).Append(campaignId).Append("/wikis/").Append(wikiId).Append(".json").ToString();
        }

    }
}
